COSA.Response = (function () {
    //region Retrieve

    function Retrieve () {
        this.businessEntity = null;
    }

    Retrieve.prototype = {
        deserialize: function ( node, context ) {
            var resultsNode = node.node('a:Results'),
                cl = new COSA.KeyValuePairCollection();

            context.extractFromNode(resultsNode);

            cl.deserialize(resultsNode, context.clone());
            this.businessEntity = cl.Entity;
        }
    };

    //endregion Retrieve

    //region RetrievePrincipalAccess

    function RetrievePrincipalAccess ( ) {
        this.isFaulted = false;
        this.error = null;
        this.accessRights = null;
    }

    RetrievePrincipalAccess.prototype = {
        deserialize: function ( node, context ) {
            var resultsNode = node.node('a:Results'),
                coll = new COSA.KeyValuePairCollection();
            context.extractFromNode(resultsNode);
            coll.deserialize(resultsNode, context.clone());
            if ( coll.hasOwnProperty('AccessRights') ) {
                this.accessRights = new COSA.AccessRights(coll.AccessRights);
            }
        }
    };

    //endregion RetrievePrincipalAccess

    //region RetrieveUserPrivileges

    function RetrieveUserPrivileges () {
        this.rolePrivileges = [];
    }

    RetrieveUserPrivileges.prototype = {
        deserialize: function ( node, context ) {
            var resultsNode = node.node('a:Results'),
                nf = new COSA.KeyValuePairCollection();
            context.extractFromNode(resultsNode);
            nf.deserialize(resultsNode, context.clone());
            this.rolePrivileges = nf.RolePrivileges.rolePrivileges;
        }
    };

    //endregion RetrieveUserPrivileges

    //region RetrieveMultiple

    function RetrieveMultiple() {
        this.entityCollection = null;
    }

    RetrieveMultiple.prototype = {
        deserialize: function ( node, context ) {
            var resultsNode = node.node('a:Results'),
                entities = new COSA.KeyValuePairCollection();
            context.extractFromNode(resultsNode);
            entities.deserialize(resultsNode, context.clone());
            this.entityCollection = entities.EntityCollection;
        }
    };

    //endregion RetrieveMultiple

    //region Delete

    function Delete ( error ) {
        this.error = typeof error != 'undefined' ? error : null;
        this.isFaulted = typeof error != 'undefined';
    }

    Delete.prototype = {
        deserialize: function ( node, context ) {
        }
    };

    //endregion Delete

    //region Update

    function Update ( error ) {
        this.error = typeof error != 'undefined' ? error : null;
        this.isFaulted = typeof error != 'undefined';
    }

    Update.prototype = {
        deserialize: function ( node, context ) {
        }
    };

    //endregion Update

    //region Create

    function Create () {
        this.id = null;
    }

    Create.prototype = {
        deserialize: function ( node, context ) {
            var resultsNode = node.node('a:Results'),
                kvcoll = new COSA.KeyValuePairCollection();
            context.extractFromNode(resultsNode);
            kvcoll.deserialize(resultsNode, context.clone());
            this.id = kvcoll.id.value;
        }
    };

    //endregion Create

    //region ExecuteMultiple

    function ExecuteMultiple () {
        this.responses = null;
        this.isFaulted = false;
        this.error = null;
    }

    ExecuteMultiple.prototype = {
        deserialize: function ( node, context ) {
            var resultsNode = node.node('a:Results'),
                results = new COSA.KeyValuePairCollection();
            context.extractFromNode(resultsNode);
            results.deserialize(resultsNode, context);
            this.responses = results.Responses;
            this.isFaulted = results.IsFaulted;
        }
    };

    //endregion ExecuteMultiple

    //region RetrieveAttribute

    function RetrieveAttribute () {
        this.attributeMetadata = null;
    }

    RetrieveAttribute.prototype = {
        deserialize: function ( node, context ) {
            var resultsNode = node.node('a:Results'),
                attrMeta = new COSA.KeyValuePairCollection();
            context.extractFromNode(resultsNode);
            attrMeta.deserialize(resultsNode, context.clone());
            this.attributeMetadata = attrMeta.AttributeMetadata;
        }
    };

    //endregion RetrieveAttribute

    //region RetrieveEntity

    function RetrieveEntity () {
        this.entityMetadata = null;
    }

    RetrieveEntity.prototype = {
        deserialize: function ( node, context ) {
            var resultsNode = node.node('a:Results'),
                meta = new COSA.KeyValuePairCollection();
            context.extractFromNode(resultsNode);
            meta.deserialize(resultsNode, context.clone());
            this.entityMetadata = meta.EntityMetadata;
        }
    };

    //endregion RetrieveEntity

    return {
        RetrieveUserPrivileges: RetrieveUserPrivileges,
        RetrieveAttribute: RetrieveAttribute,
        RetrieveEntity: RetrieveEntity,
        RetrievePrincipalAccess: RetrievePrincipalAccess,
        ExecuteMultiple: ExecuteMultiple,
        Create: Create,
        Update: Update,
        Delete: Delete,
        Retrieve: Retrieve,
        RetrieveMultiple: RetrieveMultiple
    };
})
();
